package com.example.file.controller;

import com.example.file.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileUtil fileUtil;

    @GetMapping("/spring/download")
    public ResponseEntity<Resource> download() throws Exception {
        String filePath = "D:\\1.jpg";
        String fileName = "Spring框架下载.jpg";
        return fileUtil.download(filePath,fileName);
    }

    @GetMapping("/io/download")
    public void ioDownload(HttpServletResponse response) throws Exception {
        String filePath = "D:\\1.jpg";
        String fileName = "IO下载.jpg";
        fileUtil.download(filePath,fileName,response);
    }

    @GetMapping("/tiny/download")
    public void tinyDownload(HttpServletResponse response) throws Exception {
        String filePath = "D:\\1.jpg";
        String fileName = "tiny下载.jpg";
        fileUtil.downloadTinyFile(filePath,fileName,response);
    }

    @PostMapping("/multipart/upload")
    public String download(MultipartFile file) throws Exception {
        String storagePath = "D:\\";
        return fileUtil.upload(file,storagePath);
    }
}
